package com.caiyouxi.app.service;

import com.caiyouxi.app.mapper.AppConfigMapper;
import com.caiyouxi.app.mapper.AppInfoMapper;
import com.caiyouxi.app.model.AppInfoBean;
import com.caiyouxi.app.model.AppUsersBean;
import com.caiyouxi.common.bean.QueryParams;
import com.caiyouxi.common.bean.ResData;
import com.caiyouxi.common.cache.RedisService;
import com.caiyouxi.common.exception.ErrorCode;
import com.caiyouxi.common.service.BaseService;
import com.caiyouxi.common.util.Log;
import com.caiyouxi.partner.mapper.ChannelAppConfigMapper;
import com.caiyouxi.partner.model.ChannelAppConfigBean;
import com.caiyouxi.partner.model.PartnerBean;
import com.caiyouxi.partner.service.PartnerService;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletResponse;


/**
 * APP来务相关SERVER
 *
 * @author weichangyong
 * @create 2017-10-20 15:01
 **/
@Service
public class AppInfoService  extends BaseService {

    @Autowired
    private AppInfoMapper appInfoMapper;
    @Autowired
    private AppConfigMapper appConfigMapper;
    @Autowired
    private ChannelAppConfigMapper channelAppConfigMapper;
    @Autowired
    private PartnerService partnerService;
    @Autowired
    RedisService redisService;

    private Logger logger = Logger.getLogger(AppInfoService.class);
    public AppInfoBean get_app_by_id(Integer app_id) {
        return appInfoMapper.get_app_by_id(app_id);
    }

    public ResData get_user_token(AppUsersBean bean, HttpServletResponse response) {
        try {
            AppUsersBean app_user;
            //检查应用状态
            AppInfoBean app_info_bean = appInfoMapper.get_app_by_id(bean.getApp_id());
            if (null == app_info_bean) {
                Log.e("app 不存在...");
                return failed(ErrorCode.SDK_OBJECT_NOT_FOUND.getCode(), ErrorCode.SDK_OBJECT_NOT_FOUND.getMsg(), null);
            }
            if (!"ON".equals(app_info_bean.getStatus())) {
                //如果是下架
                Log.e("app 状态异常...");
                return failed(ErrorCode.SDK_OBJECT_STATUS_ERROR.getCode(), ErrorCode.SDK_OBJECT_STATUS_ERROR.getMsg(), null);
            }
            //检查渠道状态
            PartnerBean pbean = partnerService.get_partner_by_id(bean.getChannel_id());
            if (null == pbean) {
                Log.e("渠道不存在...");
                return failed(ErrorCode.SDK_OBJECT_NOT_FOUND.getCode(), ErrorCode.SDK_OBJECT_NOT_FOUND.getMsg(), null);
            }
            if (!"NORMAL".equals(pbean.getStatus())) {
                //渠道非正常状态
                Log.e("渠道非正常状态...");
                return failed(ErrorCode.SDK_OBJECT_STATUS_ERROR.getCode(), ErrorCode.SDK_OBJECT_STATUS_ERROR.getMsg(), null);
            }
        } catch (Exception e) {
            Log.e(e.getMessage());
            return failed(ErrorCode.SDK_VERIFY_FAILD.getCode(), ErrorCode.SDK_VERIFY_FAILD.getMsg(), null);
        }
        return null;
    }

    /**
     * 获取应用配置
     * @param bean
     * @return
     */
    public ResData get_app_config(AppUsersBean bean)
    {
       return success(appConfigMapper.select_appConfig(bean.getApp_id()));
    }

    /**
     * 查询channel_app_config
     * */
    public ResData get_channel_config_list(ChannelAppConfigBean bean){
        Integer pageSize = Integer.parseInt(redisService.get("sys.pagesize").toString());//后台获取默认每页多少调数据
        QueryParams params = new QueryParams();
        params.setTotal(channelAppConfigMapper.get_count(bean));
        params.setList(channelAppConfigMapper.get_channel_config_list(bean));
        params.setPage_size(pageSize);
        params.setPage(bean.getPage());
        return success(channelAppConfigMapper.get_channel_config_list(bean));
    }

    /**
     * 新增数据
     * */
    public  ResData add_channel_config(ChannelAppConfigBean bean){
        return success(channelAppConfigMapper.add_channel_config(bean));
    }
    /**
     * 删除数据
     * */
    public ResData delete_channel_config(ChannelAppConfigBean bean){
        return success(channelAppConfigMapper.delete_channel_config(bean));
    }

}
